import {Toast} from 'vant'
import UserAPI from '../../repo/api/UserAPI'
import PlanAPI from '../../repo/api/PlanAPI'
import store from '../../repo/store/store'

//领取证书
//callBack status 1为领取成功  2为未绑定身份证  3查看证书
// 4未完成课程测试 有大考     5未完成课程测试 无大考      6已完成课程测试 未完成大考
const receiveCertificate = (planInfo, callBack) => {
  if (planInfo.certInfo.isReceive == 1) {
    //查看证书
    callBack(3)
  } else if (planInfo.subjectPassStatus === 0) {
    if(planInfo.overallExamIds && planInfo.overallExamIds.length !== 0){
      callBack(4)
    }else{
      callBack(5)
    }
  } else if (planInfo.planPassStatus === 0) {
    callBack(6)
  } else {
    UserAPI.verifyIDCard().then((result) => {
      if (result.responseData) {
        //已绑定身份证
        PlanAPI.submitCertificate(planInfo.id, planInfo.certInfo.id).then((result) => {
          let data = result.responseData
          if (data.status == 1) {
            Toast('领取成功')
            callBack(1)
          } else {
            Toast(data.msg)
          }
        })
      } else {
        //未绑定身份证
        callBack(2)
      }

    })
  }


}
//身份证绑定
const bindIDCard = (name, number, callBack) => {

  Toast.loading({
    duration: 0,
    message: '正在绑定',
    forbidClick: true,
  })

  UserAPI.bindIDCard(name, number).then((result) => {
    Toast.clear()
    Toast('绑定成功')
    callBack()
  }).catch((err) => {
    Toast.clear()
    Toast(err.result.errorInfo)
  })

}

export default {

  receiveCertificate,
  bindIDCard

}
